using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;

namespace Channelvn.Storage
{
    public class Caching
    {
        protected static bool itemRemoved = false;
        protected static CacheItemRemovedReason reason;
        protected static CacheItemRemovedCallback onRemove = null;

        public void RemovedCallback(String k, Object v, CacheItemRemovedReason r)
        {
            itemRemoved = true;
            reason = r;
        }

        public void Set(string key, object value)
        {
            itemRemoved = false;
            key = ConfigurationManager.AppSettings["SecretKey"] + "_" + key;
            onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
            HttpRuntime.Cache.Remove(key);
            HttpRuntime.Cache.Add(key, value, null, DateTime.Now.AddYears(1), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
        }

        public object Get(string key) { 
            key = ConfigurationManager.AppSettings["SecretKey"] + "_" + key;
            return HttpRuntime.Cache[key];
        }

        public void Remove(string key) {
            key = ConfigurationManager.AppSettings["SecretKey"] + "_" + key;
            HttpRuntime.Cache.Remove(key);
        }
    }
}
